package com.order.dao;

import com.order.Utils;
import com.order.pojo.Customer;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import javax.annotation.Generated;
import java.util.List;

@Repository
@Mapper
public interface CustomerDao {
    //注册(自助)
    //INSERT INTO `customer` (`name`, `phone`, `password`, `flavor`) VALUES ('向华强', '18812345678', 'hongkang', '甜');
    @Insert("INSERT INTO customer (name,phone,password,flavor,taboo) VALUES(#{name},#{phone},#{password},#{flavor},#{taboo})")
    Boolean submit(Customer customer);

    //登录(通过手机号,返回ID) useGeneratedKeys="true" keyProperty="id
    // @SelectKey(statement = "select last_insert_id()" ,keyProperty = "id",keyColumn = "id",resultType = long.class,before = false)
    //SELECT id FROM customer WHERE phone="13300000000" AND password="woaini";
    @Select("SELECT * FROM customer WHERE phone = #{phone} AND password = #{password} LIMIT 1")
    //Integer login(@Param("phone") String phone, @Param("password") String password);
    //修改逻辑，登录成功之后返回该用户的所有数据
    Customer login(String phone, String password);
    //修改密码 UPDATE customer SET password = "1234" WHERE phone="13300000000" AND password="woaini";
    @Update("UPDATE customer SET password =#{newPass}  WHERE phone=#{phone} AND password=#{oldPass}")
    Boolean updatePassword(@Param("phone") String phone,@Param("oldPass") String oldPass,@Param("newPass") String newPass);
    //充值(管理员) Recharge
    //UPDATE customer SET balance = balance+50 WHERE id=?;
    @Update("UPDATE customer SET balance = balance+#{money} WHERE id=#{id}")
    Boolean recharge(Integer id,Double money);
    //付款
    @Update("UPDATE customer SET balance = balance-#{money} WHERE id=#{id}")
    Boolean pay(Integer id,Double money);

    //安卓端修改爱好和忌口，注册得时候可以不填,flavor不为空则修改flavor,taboo不为空则修改taboo
    @UpdateProvider(type =UpdateFlavorAndTaboo.class,method = "sql")
    Boolean updateFlavorAndTaboo(Customer customer);
    //查询所有顾客的信息(管理员)
    @Select("SELECT * FROM customer")
    List<Customer> findAllCustomers();
    //修改爱好和忌口的内部类
    class UpdateFlavorAndTaboo{
        String SQL="UPDATE customer SET";
        public String sql(Customer customer)
        {
            if(customer.getTaboo()!=null)
            {
                SQL+=" taboo=#{taboo},";
            }
            if(customer.getFlavor()!=null)
            {
                SQL+=" flavor=#{flavor},";
            }
            SQL=Utils.trim(SQL)+" WHERE id=#{id}";
            return SQL;
        }

    }

}
